package dframe.ranger.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ranger.admin.client.datatype.RESTResponse;
import org.apache.ranger.plugin.model.RangerPolicy;
import org.apache.ranger.plugin.util.RangerRESTUtils;
import org.apache.ranger.plugin.util.ServicePolicies;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

import dframe.ranger.client.model.VXGroup;
import dframe.ranger.client.model.VXUser;
import dframe.ranger.client.model.VXUserGroupInfo;

public class DframeRangerClient {
	
	private static final String EXPECTED_MIME_TYPE = "application/json";
	private static final String RANGER_REPOS_URL = "http://192.168.0.177:6080";

	/**
	 * Get Policy by service-name and policy-name
	 */
	public static void testGetPolicy() {
		String url = RANGER_REPOS_URL + "/service/public/v2/api/service/hadoopdev/policy/kms-audit-path";

		Client client = null;
		ClientResponse response = null;
		try {  
	        client = Client.create();
	        client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));  
            WebResource webResource = client.resource(url);  
            response = webResource.accept(EXPECTED_MIME_TYPE).get(ClientResponse.class);  
            if(response.getStatus() == 200) {  
                String jsonString = response.getEntity(String.class);  
                System.out.println(jsonString);  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }
            if(client != null) {  
                client.destroy();  
            }  
        }  
    }

	public static void testDownload() {
		String url = RANGER_REPOS_URL + "/service/plugins/policies/download/hivedev";  
  
        ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            WebResource webResource = client.resource(url)  
                    .queryParam(RangerRESTUtils.REST_PARAM_LAST_KNOWN_POLICY_VERSION, Long.toString(68))  
                    .queryParam(RangerRESTUtils.REST_PARAM_PLUGIN_ID, "aaa");  
            response = webResource.accept(RangerRESTUtils.REST_MIME_TYPE_JSON).get(ClientResponse.class);  
  
            if (response != null && response.getStatus() == 200) {  
                ServicePolicies ret = response.getEntity(ServicePolicies.class);  
                System.out.println(ret);  
            } else if (response != null && response.getStatus() == 304) {  
                // no change  
            	System.out.println("aaaaaaaaa");  
            } else {  
                RESTResponse resp = RESTResponse.fromClientResponse(response);  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        }  
    }  
  
    private static RangerPolicy policyBlongToHive() {  
        RangerPolicy rangerPolicy = new RangerPolicy();  
        rangerPolicy.setService("hive-dev");  
        rangerPolicy.setName("project-all-path");  
        rangerPolicy.setIsAuditEnabled(true);  
  
        Map<String, RangerPolicy.RangerPolicyResource> resources = new HashMap<>();  
  
        RangerPolicy.RangerPolicyResource rangerPolicyResource = new RangerPolicy.RangerPolicyResource();  
        rangerPolicyResource.setIsExcludes(false);  
        rangerPolicyResource.setIsRecursive(true);  
        rangerPolicyResource.setValue("*");  
  
        resources.put("database", rangerPolicyResource);  
        resources.put("table", rangerPolicyResource);  
        resources.put("column", rangerPolicyResource);  
  
        List<RangerPolicy.RangerPolicyItem> policyItems = new ArrayList<>();  
  
        RangerPolicy.RangerPolicyItem rangerPolicyItem = new RangerPolicy.RangerPolicyItem();  
        List<String> users = new ArrayList<>();  
        users.add("zune");  
        rangerPolicyItem.setUsers(users);  
  
        List<RangerPolicy.RangerPolicyItemAccess> rangerPolicyItemAccesses = new ArrayList<>();  
        RangerPolicy.RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicy.RangerPolicyItemAccess();  
        rangerPolicyItemAccess.setType("select");  
        rangerPolicyItemAccess.setIsAllowed(Boolean.TRUE);  
        rangerPolicyItemAccesses.add(rangerPolicyItemAccess);  
  
        rangerPolicyItem.setAccesses(rangerPolicyItemAccesses);  
  
        policyItems.add(rangerPolicyItem);  
  
        rangerPolicy.setPolicyItems(policyItems);  
        rangerPolicy.setResources(resources);  
        return rangerPolicy;  
    }
    
    private static RangerPolicy policyBlongToHdfs() {  
        RangerPolicy rangerPolicy = new RangerPolicy();  
        rangerPolicy.setService("hadoopdev");  
        rangerPolicy.setName("mx_test_-1267765743_ALL");  
        rangerPolicy.setIsAuditEnabled(true); 
        rangerPolicy.setPolicyType(0);
        rangerPolicy.setDescription("/mx_test/input ALL");
  
        Map<String, RangerPolicy.RangerPolicyResource> resources = new HashMap<>();  
  
        RangerPolicy.RangerPolicyResource rangerPolicyResource = new RangerPolicy.RangerPolicyResource();  
        rangerPolicyResource.setIsExcludes(false);  
        rangerPolicyResource.setIsRecursive(true);  
        rangerPolicyResource.setValue("/mx_projects/mx_test/input");  
  
        resources.put("path", rangerPolicyResource);  
  
        List<RangerPolicy.RangerPolicyItem> policyItems = new ArrayList<>();  
  
        RangerPolicy.RangerPolicyItem rangerPolicyItem = new RangerPolicy.RangerPolicyItem();  
        List<String> users = new ArrayList<>();  
        rangerPolicyItem.setUsers(users);
        
        List<String> userGroups = new ArrayList<String>();
//        userGroups.add("public");
//        userGroups.add("hadoop");
        rangerPolicyItem.setGroups(userGroups);
  
        List<RangerPolicy.RangerPolicyItemAccess> rangerPolicyItemAccesses = new ArrayList<>();  
        RangerPolicy.RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicy.RangerPolicyItemAccess();  
        rangerPolicyItemAccess.setType("read");  
        rangerPolicyItemAccess.setIsAllowed(Boolean.TRUE);  
        rangerPolicyItemAccesses.add(rangerPolicyItemAccess);  
  
        rangerPolicyItem.setAccesses(rangerPolicyItemAccesses);  
  
        policyItems.add(rangerPolicyItem);  
  
        rangerPolicy.setPolicyItems(policyItems);  
        rangerPolicy.setResources(resources);
        
        return rangerPolicy;  
    }
    
    /**
     * Create Policy
     */
    public static void testCreatePolicy() {  
  
        String url = RANGER_REPOS_URL + "/service/public/v2/api/policy";  
  
        ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
  
            WebResource webResource = client.resource(url); 
  
            Gson gson = new Gson();  
            System.out.println(gson.toJson(policyBlongToHive()));
  
            response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, gson.toJson(policyBlongToHive()));
//                    .post(ClientResponse.class, gson.toJson(policyBlongToHdfs()));  
  
            if (response != null && response.getStatus() == 200) {
            	System.out.println(response.toString());
                RangerPolicy ret = response.getEntity(RangerPolicy.class);  
                System.out.println(ret);  
            } else {  
                System.out.println(response.getStatus());  
            } 
        }catch(Exception ex) {
        	ex.printStackTrace();
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        }  
  
    }  
  
    public static void testUpdatePolicy() {  
  
        String url = RANGER_REPOS_URL + "/service/public/v2/api/policy/11";  
  
        RangerPolicy rangerPolicy = policyBlongToHdfs();  
        rangerPolicy.getPolicyItems().get(0).getUsers().add("june");  
  
  
        ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));  
  
            WebResource webResource = client.resource(url);  
  
            Gson gson = new Gson();  
  
            response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .put(ClientResponse.class, gson.toJson(rangerPolicy));  
  
            if (response != null && response.getStatus() == 200) {  
                RangerPolicy ret = response.getEntity(RangerPolicy.class);  
                System.out.print(ret.getId());  
                System.out.println(ret);  
            } else {  
                System.out.println(response.getStatus());  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        }  
    }  
  
    public static void testDeletepolicy() {  
        String url = RANGER_REPOS_URL + "/service/public/v2/api/policy/29";  
  
  
        ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));  
  
            WebResource webResource = client.resource(url);  
  
            webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).delete();  
  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        }  
    }
    
    //==========================================user group test
	private static VXUser createVXUser() {
		VXUser testVXUser= new VXUser();
		Collection<String>c = new ArrayList<String>();
		testVXUser.setCreateDate(new Date());
		testVXUser.setUpdateDate(new Date());
		testVXUser.setOwner("Admin");
		testVXUser.setUpdatedBy("Admin");
		testVXUser.setName("june-api-test");
		testVXUser.setFirstName("F-api-test");
		testVXUser.setLastName("L-api-test");
		testVXUser.setPassword("123456");
		
		Collection<Long> groupIdList = new ArrayList<Long>();
		groupIdList.add(36L);
		testVXUser.setGroupIdList(groupIdList);
		//给用户添加组不好使 36
		Collection<String> groups = new ArrayList<String>();
		groups.add("june-test-Group5");
		testVXUser.setGroupNameList(groups);
		testVXUser.setStatus(1);
		testVXUser.setIsVisible(1);
		testVXUser.setUserSource(0);
		c.add("ROLE_USER");
		testVXUser.setUserRoleList(c);
		
		return testVXUser;
	}
	
	public static void testCreateUser() {
		String url = RANGER_REPOS_URL + "/service/xusers/secure/users";
   	 
   	 	ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
  
            WebResource webResource = client.resource(url); 
  
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
            System.out.println(gson.toJson(createVXUser()));
  
            response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, gson.toJson(createVXUser()));  
  
            if (response != null && response.getStatus() == 200) {
            	System.out.println(response.toString());
//                RangerPolicy ret = response.getEntity(RangerPolicy.class);  
//                System.out.println(ret);  
            } else {  
                System.out.println(response.toString());  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        } 
	}
	
	 public static void testGetUser() {
     	 String url = RANGER_REPOS_URL + "/service/xusers/users/userName/june-api-test";
     	 
     	 ClientResponse response = null;  
          Client client = null;  
          try {  
              client = Client.create();  
              client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
    
              WebResource webResource = client.resource(url); 
    
              Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
    
              response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                      .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                      .get(ClientResponse.class);
    
              if (response != null && response.getStatus() == 200) {
              		System.out.println(response.toString());
              		VXUser ret = response.getEntity(VXUser.class);
              		
              		System.out.println(ret.getName() + ":::" + ret.getId());
              } else {  
                  System.out.println(response.toString());  
              }  
          } finally {  
              if(response != null) {  
                  response.close();  
              }  
              if(client != null) {  
                  client.destroy();  
              }  
          } 
     }
	
	public static void testUpdateUser() {
		String url = RANGER_REPOS_URL + "/service/xusers/secure/users";
   	 
   	 	ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
  
            WebResource webResource = client.resource(url); 
  
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
            System.out.println(gson.toJson(createVXUser()));
  
            response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, gson.toJson(createVXUser()));  
  
            if (response != null && response.getStatus() == 200) {
            	System.out.println(response.toString());
//                RangerPolicy ret = response.getEntity(RangerPolicy.class);  
//                System.out.println(ret);  
            } else {  
                System.out.println(response.toString());  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        } 
	}
	
    private static VXGroup createVXGroup() {
		VXGroup testVXGroup= new VXGroup();
		testVXGroup.setName("june-test-Group5");
		testVXGroup.setCreateDate(new Date());
		testVXGroup.setUpdateDate(new Date());
		testVXGroup.setUpdatedBy("Admin");
		testVXGroup.setOwner("Admin");
//		testVXGroup.setId(id);
//		testVXGroup.setCredStoreId(1L);
		
		return testVXGroup;
	}
    
    public static void testCreateGroup() {
    	 String url = RANGER_REPOS_URL + "/service/xusers/secure/groups";
    	 
    	 ClientResponse response = null;  
         Client client = null;  
         try {  
             client = Client.create();  
             client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
   
             WebResource webResource = client.resource(url); 
   
             Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
             System.out.println(gson.toJson(createVXGroup()));
   
             response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                     .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                     .post(ClientResponse.class, gson.toJson(createVXGroup()));  
   
             if (response != null && response.getStatus() == 200) {
             	System.out.println(response.toString());
//                 RangerPolicy ret = response.getEntity(RangerPolicy.class);  
//                 System.out.println(ret);  
             } else {  
                 System.out.println(response.toString());  
             }  
         } finally {  
             if(response != null) {  
                 response.close();  
             }  
             if(client != null) {  
                 client.destroy();  
             }  
         } 
    }
	
    public static VXUserGroupInfo testCreateVXUserGroupInfo() {
    	VXUserGroupInfo userGroup = new VXUserGroupInfo();
    	
    	VXUser testVXUser= new VXUser();
		testVXUser.setName("june-api-test");
		
		VXGroup testVXGroup= new VXGroup();
		testVXGroup.setName("june-test-Group5");
		
		List<VXGroup> groupList = new ArrayList<VXGroup>();
		groupList.add(testVXGroup);
		
		userGroup.setXgroupInfo(groupList);
		userGroup.setXuserInfo(testVXUser);
		
		return userGroup;
    }
    
    public static void testCreateUserGroupInfo() {
   	 	String url = RANGER_REPOS_URL + "/service/xusers/users/userinfo";
   	 
   	 	ClientResponse response = null;  
        Client client = null;  
        try {  
            client = Client.create();  
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
  
            WebResource webResource = client.resource(url); 
  
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
            System.out.println(gson.toJson(testCreateVXUserGroupInfo()));
  
            response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, gson.toJson(testCreateVXUserGroupInfo()));  
  
            if (response != null && response.getStatus() == 200) {
            	System.out.println(response.toString());
//                RangerPolicy ret = response.getEntity(RangerPolicy.class);  
//                System.out.println(ret);  
            } else {  
                System.out.println(response.toString());  
            }  
        } finally {  
            if(response != null) {  
                response.close();  
            }  
            if(client != null) {  
                client.destroy();  
            }  
        } 
   }
    
    public static void testGetUserGroupInfo() {
      	 String url = RANGER_REPOS_URL + "/service/xusers/groupusers/groupName/june-test-Group5";
      	 
      	 ClientResponse response = null;  
           Client client = null;  
           try {  
               client = Client.create();  
               client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
     
               WebResource webResource = client.resource(url); 
     
               Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
     
               response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                       .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                       .get(ClientResponse.class);
     
               if (response != null && response.getStatus() == 200) {
               		System.out.println(response.toString());
               		VXUserGroupInfo ret = response.getEntity(VXUserGroupInfo.class);
               		
               		System.out.println(ret.getXuserInfo().getName() + ":::" + ret.getXuserInfo().getId());
               		for(VXGroup group : ret.getXgroupInfo()){
               			System.out.println(group.getName() + ":::" + group.getId()); 
               		}
               } else {  
                   System.out.println(response.toString());  
               }  
           } finally {  
               if(response != null) {  
                   response.close();  
               }  
               if(client != null) {  
                   client.destroy();  
               }  
           } 
      }
    
    public static void testGetGroup() {
     	 String url = RANGER_REPOS_URL + "/service/xusers/groups/groupName/june-test-Group5";
     	 
     	 ClientResponse response = null;  
          Client client = null;  
          try {  
              client = Client.create();  
              client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
    
              WebResource webResource = client.resource(url); 
    
              Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").create();
    
              response = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)  
                      .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                      .get(ClientResponse.class);
    
              if (response != null && response.getStatus() == 200) {
              		System.out.println(response.toString());
              		VXGroup ret = response.getEntity(VXGroup.class);
              		
              		System.out.println(ret.getName() + ":::" + ret.getId());
              } else {  
                  System.out.println(response.toString());  
              }  
          } finally {  
              if(response != null) {  
                  response.close();  
              }  
              if(client != null) {  
                  client.destroy();  
              }  
          } 
     }
    
	public static void main(String[] args) {
//		testCreateUser();
//		testCreateGroup();
		
//		testGetUser();
//		testGetGroup();
		
//		testGetUserGroupInfo();
		
//		testCreateUserGroupInfo();
		testCreatePolicy();
//		testGetPolicy();
		
//		testUpdatePolicy();
		
	}

}
